#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

#define TYPE int


typedef struct Node
{
	TYPE data;
	struct Node* next;
}Node;

Node* create_node(TYPE data)
{
	Node* node = malloc(sizeof(Node));
	node->data = data;
	node->next = NULL;
	return node;
}

typedef struct Stack
{
	Node* top;
	size_t len;
}Stack;

// 创建
Stack* create_stack(void);
// 销毁
void destory_stack(Stack* stack);
// 栈空
bool empty_stack(Stack* stack);
// 入栈
void push_stack(Stack* stack,TYPE data);
// 出栈
bool pop_stack(Stack* stack);
// 栈顶
TYPE* top_stack(Stack* stack);

int main()
{
	Stack* stack = create_stack();
	for(int i=0; i<10; i++)
	{
		push_stack(stack,rand()%50);
		printf("top:%d\n",*top_stack(stack));
	}
	for(int i=0; i<10; i++)
	{
		printf("top:%d\n",*top_stack(stack));
		printf("出栈:%s\n",pop_stack(stack)?"yes":"no");
	}
}

// 创建
Stack* create_stack(void)
{
	Stack* stack = malloc(sizeof(Stack));
	stack->top = NULL;
	stack->len = 0;
	return stack;
}

// 销毁
void destory_stack(Stack* stack)
{
	while(pop_stack(stack));
	free(stack);
}
// 栈空
bool empty_stack(Stack* stack)
{
	return NULL == stack->top;
}
// 入栈
void push_stack(Stack* stack,TYPE data)
{
	Node* node = create_node(data);
	node->next = stack->top;
	stack->top = node;
	stack->len++;
}
// 出栈
bool pop_stack(Stack* stack)
{
	if(empty_stack(stack)) return false;
	Node* node = stack->top;
	stack->top = node->next;
	free(node);
	stack->len--;
	return true;
}
// 栈顶
TYPE* top_stack(Stack* stack)
{
	if(empty_stack(stack)) return NULL;
	return &stack->top->data;
}
